package wili.b1.transform;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import wili.attrMapping.file.JsonFile;
import wili.c.Ca;
import wili.json.JsonNode;
import wili.json.JsonObj;

class update_editable_by_GroupAlwaysEditable {
	public static void main(String[] args) {
		File dir = new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\fragment\\a");
		File[] ff = dir.listFiles();
		File[] ff2 = { new File(dir, "accountingTab.fragment.json"), new File(dir, "accountingTab_AR.fragment.json"),
				new File(dir, "accountingTab_BR.fragment.json") };
		List<File> list2 = Arrays.asList(ff2);
		int times = 0;
		StringBuilder sb = new StringBuilder();
		Set<String> set = new HashSet<>();
		for (File file : ff) {
			if (list2.contains(file))
				continue;
			JsonFile json = new JsonFile(file);
			//List<JsonObj> obj = json.findObjs_byAtt("name", "GroupAlwaysEditable").stream().filter(nod -> nod.valueOfSon("editableFormular", "true") != null).collect(Collectors.toList());
			List<JsonObj> obj = json.findObjs_byAtt("formulaGroup", "GroupAlwaysEditable");//.stream().filter(nod -> nod.valueOfSon("editableFormular", "true") != null).collect(Collectors.toList());
			for (JsonObj ele : obj) {
				if (set.add(ele.dad.toObj().guid))
					sb.append("|" + ele.dad.toObj().guid);
				else
					continue;
				++times;
			}
		}
		Ca.log(" set1=" + set.size() + set);
		for (File file : ff2) {
			JsonFile json = new JsonFile(file);
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				String id = it.next();
				JsonObj obj = json.findObj_byAtt("guid", id);
				if (obj == null) {
					continue;
				}
				JsonNode edit = obj.son("editable");
				if (edit == null) {
					Ca.log(file.getName() + ", edit not found:" + id);
					continue;
				}
				if (edit.stringValue.trim().equals("")) {
					Ca.log(" guid:" + id + ", editable: " + edit.stringValue);
				}
				edit.setStringValue("{=!${view>/$EditMode/viewMode}}");
				it.remove();
			}
			if ("sadf".isEmpty())
				json.saveToFile(null, null);
		}
		Ca.log(" set2=" + set.size() + set);
		Ca.log(sb.toString());
	}
}
